package com.oro.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class UsernameValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

		String enteredUsername = (String) value;

		Pattern usernamePattern = Pattern.compile("[a-z][a-zA-Z_0-9]+");

		Matcher m = usernamePattern.matcher(enteredUsername);

		if (!m.matches()) {
			FacesMessage message = new FacesMessage();
			message.setSummary("Username non valido.");
			throw new ValidatorException(message);
		}

	}

}
